package dz.common;

import java.io.UnsupportedEncodingException;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PyDigester {
    private static Logger logger = LoggerFactory.getLogger(PyDigester.class);
    private static final String DEFAULT_CHARSET = "UTF-8";
    public static final String KEY_ALGORITHM = "DES";
    public static final String CIPHER_ALGORITHM_ECB = "DES";
    public static final String ENCRYPT_CDKEY = "QK7VoW2hFsE=";

    public PyDigester() {
    }

    public static final String md5ForUser(String res, String rander) {
        try {
            return DigestUtils.md5Hex(DigestUtils.md5Hex(res.getBytes("UTF-8")) + rander);
        } catch (UnsupportedEncodingException var3) {
            logger.error("md5加密异常", var3);
            return null;
        }
    }

    public static final String sha1ForOper(String res) throws Exception {
        try {
            return DigestUtils.sha1Hex(res.getBytes("UTF-8"));
        } catch (Exception var2) {
            logger.error("sha1加密异常", var2);
            throw var2;
        }
    }

    private static String generateKey() throws Exception {
        KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
        keyGenerator.init(56);
        SecretKey secretKey = keyGenerator.generateKey();
        return Base64.encodeBase64String(secretKey.getEncoded());
    }

    private static Key toKey(String key) throws Exception {
        DESKeySpec des = new DESKeySpec(Base64.decodeBase64(key));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = keyFactory.generateSecret(des);
        return secretKey;
    }

    public static String desEncrypt(String data, String key) throws Exception {
        Key k = toKey(key);
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(1, k);
        return Base64.encodeBase64String(cipher.doFinal(data.getBytes("UTF-8")));
    }

    public static String desDecrypt(String data, String key) throws Exception {
        Key k = toKey(key);
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(2, k);
        return new String(cipher.doFinal(Base64.decodeBase64(data)));
    }

    public static String encryptCommonInfo(String resoureInfo) {
        try {
            return desEncrypt(resoureInfo, "QK7VoW2hFsE=");
        } catch (Exception var2) {
            var2.printStackTrace();
            return null;
        }
    }

    public static String decryptCommonInfo(String encodeInfo) {
        try {
            return desDecrypt(encodeInfo, "QK7VoW2hFsE=");
        } catch (Exception var2) {
            var2.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) throws Exception {
        String res = "123456";
        String key = generateKey();
        System.out.println("key:" + key);
        String en = desEncrypt(res, key);
        System.out.println("en:" + en);
        System.out.println("密文：" + DigestUtils.md5Hex(res.getBytes("UTF-8")));
        String de = desDecrypt(en, key);
        System.out.println("解密：" + de);
        System.out.println("解密是否成功：" + de.equals(res));
        System.out.println("name=" + encryptCommonInfo("dianzhuang"));
        System.out.println(decryptCommonInfo(encryptCommonInfo("")));

//        String userName = desDecrypt("MBYpuT27U/usFJYZwWKayA==", key);
        System.out.println("解密用户名：" + decryptCommonInfo("MBYpuT27U/usFJYZwWKayA=="));
        System.out.println("解密密码：" + decryptCommonInfo("88pKOU7tsqPSHwWdeuy7Zg=="));

    }
}
